#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <cstring>

void SetNonBlock(int fd) {
  // 获取原始的文件状态
  int oldfd = fcntl(fd, F_GETFL);
  if(oldfd == -1) {
    std::cout << "setFL err errno::" << errno << "err message::" << strerror(errno) << std::endl;
    exit(1);
  }
  fcntl(fd, F_SETFL, oldfd | O_NONBLOCK);
}

int main() {
  
  SetNonBlock(0);
  while(1) {
    errno = 0;
    char buff[1024];
    ssize_t e = read(0, buff, sizeof(buff) - 1);
    if(e > 0) {
      buff[e - 1] = 0;
      std::cout << "read::" << buff << " errno::" << errno << "--->" << strerror(errno) << std::endl;
    } else {

      if((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
        std::cout << "当前资源未就绪" << std::endl;
        sleep(3);
        continue;
      } else if(errno == EINTR) {
        std::cout << "当前IO可能被信号中断" << std::endl;
        sleep(3);
        continue;
      }

    }
  } 
  return 0;
}
